var modePartnerSelector = 0, modeCatsSelector = 0;

function addListenerAllPart() {
    $('#selectPartners').click(function() {
        if(modePartnerSelector == 0) {
            $(".listPartners:not(:checked)").each(function() {
                $(this).attr('checked', true);
            });
            modePartnerSelector = 1;
        } else {
            $('.listPartners:checked').each(function() {
                $(this).attr('checked', false);
            });
            modePartnerSelector = 0;
        }
    });
}

function loadPartners() {
    var catsid = '';
    $('.catsList:checked').each(function(){
        catsid += $(this).val() + ','; 
    });
    if(catsid != '') {
        $('.forumsList').load('/ajax/getForumList', {'catsid':catsid, 'modelName':'User'}, function() {
            addListenerAllPart();
        });
    } else $('.forumsList').empty();    
}

$(document).ready(function() {
    $('.standartRoles').change(function() {
        if($(this).val() == 'moder') {
            $('.moderForums').css('display', 'block');
        } else $('.moderForums').css('display', 'none');
    });
    
    $('.catsList').click(function() {
        loadPartners();
    });
    
    $('#selectCats').click(function(){
        if(modeCatsSelector == 0) {
            $(".catsList:not(:checked)").each(function() {
                $(this).attr('checked', true);
            });
            modeCatsSelector = 1;
        } else {
            $('.catsList:checked').each(function() {
                $(this).attr('checked', false);
            });
            modeCatsSelector = modePartnerSelector = 0;
        }
        loadPartners();
    });
    
    addListenerAllPart();
});